package com.stephen.demo.service.impl;

import com.stephen.demo.entity.Role;
import com.stephen.demo.entity.User;
import com.stephen.demo.mapper.UserDao;
import com.stephen.demo.service.UserService;
import com.stephen.demo.utils.SaltUtil;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * Created by stephen on 2021-04-05 16:18 .
 * Description:
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public Set<Role> getRoleByUsername(String username) {
        return userDao.getRoleByUsername(username);
    }

    @Override
    public Set<String> getPermsByRoleId(Integer roleId) {
        return userDao.getPermsByRoleId(roleId);
    }

    @Override
    public void save(User user) {
        String salt = SaltUtil.getSalt(8);
        // 对密码进行md5 + salt + hash
        Md5Hash md5Hash = new Md5Hash(user.getPassword(), salt, 1024);
        user.setSalt(salt);
        user.setPassword(md5Hash.toHex());
        userDao.save(user);
    }

    @Override
    public User findByUserName(String username) {
        return userDao.findByUserName(username);
    }
}
